package eventStrategies;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

import eventManager.AppEvent;
import eventManager.IEventListener;
import gameAppLayer.RootNodePortal;

public class ChangeViewStrategy implements IEventListener {

	private HashMap<String, Node> nodes;
	private HashSet<String> eventTypes = new HashSet<String>();
	
	public ChangeViewStrategy(HashMap<String, Node> nodes) {
		eventTypes.add("ChangeView");
		this.nodes = nodes;
	}

	public void handleEvent(AppEvent event) {
//		boolean changed = false;
		
		Object[] objs = event.getEventObjects();
		String newView = (String) objs[0];
		
		Node nodeTest = nodes.get(newView);
		
		if (nodeTest!=null) {
			//cycle through list of all nodes, ensure they are detached from rootNode
			Set<String> nodeNames = nodes.keySet();
			Node tempNode;
			
			/*
			 * QQQ:NOTE that this assumes direct attaching to rootNode.  Cannot handle multiple nodes strung together,
			 * as it will detach them from each other.
			 */
			for (String name: nodeNames) {
				tempNode = nodes.get(name);
				
				if (RootNodePortal.hasChild(tempNode)){
					tempNode.removeFromParent();					
				}								
			}
			
			RootNodePortal.attachChild(nodeTest);	
		}
	}
	
	public Set<String> getEventTypes() {
		return eventTypes;
	}
	

}
